package com.whq.thrift.api.web.admin.controller.bs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.urlmapping.UrlMappingInfo;
import com.whq.thrift.api.web.admin.service.baseconf.UrlMappingInfoService;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class UrlMappingInfoController {
	private static final int DEFAULT_PAGE_SIZE = 25;

	@Autowired
	private UrlMappingInfoService urlMappingInfoService;

	@RequestMapping(value = "bs/urlmappinginfo/list")
	public String list(@RequestParam(value = "url", required = false) String url,
			@RequestParam(value = "currentPage", required = true, defaultValue = "1") Integer currentPage,
			@RequestParam(value = "pageSize", required = false) Integer pageSize, Model model) {

		PageData<UrlMappingInfo> pageData = new PageData<UrlMappingInfo>();
		pageData.setCurrentPage(currentPage);
		if ((pageSize == null) || (pageSize <= 0)) {
			pageSize = DEFAULT_PAGE_SIZE;
		}
		pageData.setPageSize(pageSize);

		// 查询参数
		Map<String, Object> whereParams = new HashMap<String, Object>(0);
		if (StringUtil.isNotBlank(url)) {
			whereParams.put("url", url);
		}

		int totalCount = urlMappingInfoService.findUrlMappingInfoCount(whereParams);
		pageData.setTotalCount(totalCount);

		int beginIndex = (currentPage - 1) * pageSize;
		List<UrlMappingInfo> resultList = urlMappingInfoService.findUrlMappingInfoByMap(whereParams, beginIndex, pageSize);
		pageData.setResultList(resultList);

		model.addAttribute("url", url);
		model.addAttribute("pageData", pageData);
		return "bs/urlmappinginfo/list";
	}

	@RequestMapping(value = "bs/urlmappinginfo/dels")
	public String deleteUrlMappingInfos(String[] urls, Model model) {
		if (urls != null) {
			for (int i = 0; i < urls.length; i++) {
				if (urls[i] != null) {
					this.urlMappingInfoService.deleteUrlMappingInfo(urls[i]);
				}
			}
		}
		return "redirect:list.do";
	}

	@RequestMapping(value = "bs/urlmappinginfo/del")
	public String del(@RequestParam(value = "url", required = true, defaultValue = "0") String url, Model model) {
		this.urlMappingInfoService.deleteUrlMappingInfo(url);
		return "bs/urlmappinginfo/delOk";
	}

	@RequestMapping(value = "bs/urlmappinginfo/showAdd")
	public String showAdd(Model model) {
		UrlMappingInfo urlMappingInfo = new UrlMappingInfo();
		model.addAttribute("urlMappingInfo", urlMappingInfo);
		return "bs/urlmappinginfo/showAdd";
	}

	@RequestMapping(value = "bs/urlmappinginfo/add")
	public String add(@Valid UrlMappingInfo urlMappingInfo, BindingResult result, Model model) {
		UrlMappingInfo existUrlMappingInfo = this.urlMappingInfoService.findUrlMappingInfo(urlMappingInfo.getUrl());
		if (existUrlMappingInfo != null) {

			model.addAttribute("urlMappingInfo", urlMappingInfo);
			return "bs/urlmappinginfo/showAdd";
		}

		this.urlMappingInfoService.addUrlMappingInfo(urlMappingInfo);
		return "redirect:show.do?url=" + urlMappingInfo.getUrl();
	}

	@RequestMapping(value = "bs/urlmappinginfo/show")
	public String show(@RequestParam(value = "url", required = true) String url, Model model) {
		if (StringUtil.isEmpty(url)) {
			return "error";
		}
		UrlMappingInfo urlMappingInfo = this.urlMappingInfoService.findUrlMappingInfo(url);
		model.addAttribute("urlMappingInfo", urlMappingInfo);
		if (urlMappingInfo == null) {
			return "error";
		}
		return "bs/urlmappinginfo/show";
	}

	@RequestMapping(value = "bs/urlmappinginfo/showUpd")
	public String showUpd(@RequestParam(value = "url", required = true) String url, Model model) {
		if (StringUtil.isEmpty(url)) {
			return "error";
		}
		UrlMappingInfo urlMappingInfo = this.urlMappingInfoService.findUrlMappingInfo(url);
		if (urlMappingInfo == null) {
			return "error";
		}
		model.addAttribute("urlMappingInfo", urlMappingInfo);
		return "bs/urlmappinginfo/showUpd";
	}

	@RequestMapping(value = "bs/urlmappinginfo/upd")
	public String upd(@Valid UrlMappingInfo urlMappingInfo, BindingResult result, Model model) {
		if (result.hasErrors()) {
			model.addAttribute("urlMappingInfo", urlMappingInfo);
			return "bs/urlmappinginfo/showUpd";
		}

		this.urlMappingInfoService.updateUrlMappingInfo(urlMappingInfo);
		return "redirect:show.do?url=" + urlMappingInfo.getUrl();
	}
}
